//
//  TravelDiaryDetailViewController.m
//  NineGroupProject
//
//  Created by dllo on 15/12/25.
//  Copyright © 2015年 lushan. All rights reserved.
//

#import "TravelDiaryDetailViewController.h"
#import "Masonry.h"
#import "UIImageView+WebCache.h"
#import "TravelFirstTableViewCell.h"
#import "LSAppTools.h"
#import "LabelTableViewCell.h"
#import "UsefulTableViewCell.h"
#import "Attraction_trip_tags.h"
#import "TravelDetailModel.h"
#import "ShowTableViewCell.h"
#import "SearchViewController.h"
#import "TravelPhotoViewController.h"

@interface TravelDiaryDetailViewController ()<UITableViewDataSource, UITableViewDelegate, foreignDetailCellDelegate>
@property (nonatomic, strong) UITableView *myTableView;
@property (nonatomic, copy) NSString *myDescriptions;
@property (nonatomic, copy) NSString *usefulText;
@property (nonatomic, strong) NSMutableArray *array;
@end

@implementation TravelDiaryDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIImage *leftImage = [UIImage imageNamed:@"iconfont-fanhui-2.png"];
    leftImage = [leftImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:leftImage style:UIBarButtonItemStyleDone target:self action:@selector(leftButtonAction)];
    
    self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 69 *HEI) style:UITableViewStylePlain];
    [self.view addSubview:self.myTableView];
    
    UIImageView *imageV = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 220 * HEI)];
    self.myTableView.tableHeaderView = imageV;
    imageV.backgroundColor = [UIColor blueColor];
//    [imageV mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.and.top.mas_equalTo(0);
//        make.width.mas_equalTo(WIDTH);
//        make.height.mas_equalTo(220 * HEI);
//        
//    }];
    [imageV sd_setImageWithURL:[NSURL URLWithString:self.diaryModel.image_url] placeholderImage:[UIImage imageNamed:@"loading.jpg"]];
    

    
    UILabel *ChineseNameLabel = [[UILabel alloc] init];
    [imageV addSubview:ChineseNameLabel];
    [ChineseNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(50 * WID);
        make.bottom.mas_equalTo(-30 * HEI);
        make.width.mas_equalTo(300 * WID);
        make.height.mas_equalTo(30 * HEI);
        
    }];
    ChineseNameLabel.text = self.diaryModel.name;
    ChineseNameLabel.textColor = [UIColor whiteColor];
    
    UILabel *EnglishNameLabel = [[UILabel alloc] init];
    [imageV addSubview:EnglishNameLabel];
    [EnglishNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(ChineseNameLabel.mas_left);
        make.top.equalTo(ChineseNameLabel.mas_bottom);
        make.width.equalTo(ChineseNameLabel);
        make.height.equalTo(ChineseNameLabel);
        
    }];
    EnglishNameLabel.text= self.diaryModel.name_en;
    EnglishNameLabel.textColor = [UIColor whiteColor];
    
    
    UIImageView *titleImage = [[UIImageView alloc] init];
    [imageV addSubview:titleImage];
    [titleImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(ChineseNameLabel.mas_left).offset(-5 * WID);
        make.top.equalTo(ChineseNameLabel.mas_top).offset(5 * HEI);
        make.width.mas_equalTo(40 * WID);
        make.height.mas_equalTo(50 * HEI);
        
    }];
    titleImage.image = [UIImage imageNamed:@"iconfont-jingdian.png"];
    
    
    self.myTableView.dataSource = self;
    self.myTableView.delegate = self;
    
    [self.myTableView registerClass:[TravelFirstTableViewCell class] forCellReuseIdentifier:@"TravelFirstTableViewCell"];
//    [self.myTableView registerClass:[UsefulTableViewCell class] forCellReuseIdentifier:@"UsefulTableViewCell"];
    [self.myTableView registerClass:[ShowTableViewCell class] forCellReuseIdentifier:@"ShowTableViewCell"];
    
    self.array = [NSMutableArray array];
    [self getAFNetWorkingData];
    

}

- (void)getAFNetWorkingData{
    NSLog(@"%@",self.diaryModel.myID);
        NSString *url_string = [NSString stringWithFormat:@"http://chanyouji.com/api/attractions/%@.json?page=1",self.diaryModel.myID];
        [LSAppTools getDataWithURL:url_string blcok:^(id result) {
            self.myDescriptions = [result objectForKey:@"description"];
            self.usefulText = [result objectForKey:@"tips_html"];
            NSMutableArray *bigArr = [result objectForKey:@"attraction_trip_tags"];
            for (NSMutableDictionary *dic in bigArr) {
                Attraction_trip_tags *attraction_trip_tags = [[Attraction_trip_tags alloc] init];
                [attraction_trip_tags setValuesForKeysWithDictionary:dic];
                [self.array addObject:attraction_trip_tags];
                for (TravelDetailModel *model in attraction_trip_tags.attraction_contents) {

                    model.height = [LabelTableViewCell heightForContent:model.placeDescription];
                    
                    
                }
                
            }
            
            [self.myTableView reloadData];
            
            
        }];
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return 0;
    }else{
        return 30;
    }
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.array.count + 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section < 1) {
        return 1;
    }else{
        Attraction_trip_tags *attraction_trip_tags = [self.array objectAtIndex:section - 1];
        return attraction_trip_tags.attraction_contents.count;
    }
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        TravelFirstTableViewCell *diaryTableCell = [tableView dequeueReusableCellWithIdentifier:@"TravelFirstTableViewCell"];
        diaryTableCell.cellDelegate = self;
        diaryTableCell.selectionStyle = UITableViewCellSelectionStyleNone;
        diaryTableCell.descriptions = self.myDescriptions;
        diaryTableCell.diaryModel = self.diaryModel;
        return diaryTableCell;
    }else{
        ShowTableViewCell *showCell = [tableView dequeueReusableCellWithIdentifier:@"ShowTableViewCell"];
        Attraction_trip_tags *attraction_trip_tags = [self.array objectAtIndex:indexPath.section - 1];
        showCell.travelDetail = [attraction_trip_tags.attraction_contents objectAtIndex:indexPath.row];
        showCell.selectionStyle = UITableViewCellSelectionStyleNone;
        return showCell;
        
//        UsefulTableViewCell *usefulCell = [tableView dequeueReusableCellWithIdentifier:@"UsefulTableViewCell"];
//        usefulCell.userfulText = self.usefulText;
//        usefulCell.selectionStyle = UITableViewCellSelectionStyleNone;
//        return usefulCell;
    }
    
}

//-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//    [tableView deselectRowAtIndexPath:indexPath animated:YES];
//    
//}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        CGFloat height = [LabelTableViewCell heightForContent:self.myDescriptions];
        
        return height + 55 * HEI + 10;
    }else{
//        CGFloat height = [LabelTableViewCell heightForContent:self.usefulText];
//        return height + 10;
        Attraction_trip_tags *attraction_trip_tags = [self.array objectAtIndex:indexPath.section - 1];
        TravelDetailModel *travelDetail = [attraction_trip_tags.attraction_contents objectAtIndex:indexPath.row];
        return travelDetail.height + 150;
    }
   
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    if (section > 0) {
        Attraction_trip_tags *attraction_trip_tags = [self.array objectAtIndex:section - 1];
        return attraction_trip_tags.name;;
    }else{
        return nil;
    }
}
-(void)leftButtonAction{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)pushToViewControllerTag:(NSInteger)tag DiaryModel:(DiaryModel *)diaryModel{
    if (tag == 1000) {
        TravelPhotoViewController *travelPhotoVC = [[TravelPhotoViewController alloc] init];
        travelPhotoVC.diaryModel = diaryModel;
        [self.navigationController pushViewController:travelPhotoVC animated:YES];
    }
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
